package com.ruoyi.web.controller.content;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.shop.modules.domain.ProductCategory;
import com.ruoyi.shop.modules.domain.vo.DataInfo;
import com.ruoyi.shop.modules.service.DataService;
import com.ruoyi.shop.modules.service.IOrderService;
import com.ruoyi.shop.modules.service.IProductCategoryService;
import com.ruoyi.shop.modules.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @Author: JinChen Zhu
 * @CreateTime: 2024-07-03  12:48
 * @Description: 可视化数据接口
 */
@RestController
@RequestMapping("/data")
public class DataController extends BaseController {
    @Autowired
    private DataService dataService;
    @Autowired
    private IProductService productService;
    @Autowired
    private IProductCategoryService productCategoryService;
    @Autowired
    private IOrderService orderService;


    @GetMapping("/user/count")
    public AjaxResult getUserCount() {
        List<DataInfo> dataInfos = new ArrayList<>();
        dataInfos.add(new DataInfo("用户", (long) dataService.getAllCount()));
        return AjaxResult.success(dataInfos);
    }
    @GetMapping("/user/province")
    public AjaxResult getProvince() {
        List<String> provinces = Arrays.asList(
                "北京", "天津", "上海", "重庆",
                "河北", "山西", "辽宁", "吉林", "黑龙江",
                "江苏", "浙江", "安徽", "福建", "江西",
                "山东", "河南", "湖北", "湖南", "广东",
                "海南", "四川", "贵州", "云南", "陕西",
                "甘肃", "青海", "台湾",
                "内蒙古", "广西", "西藏", "宁夏", "新疆",
                "香港", "澳门"
        );
        List<DataInfo> dataInfos = new ArrayList<>();

        for (String province : provinces) {
            int count = dataService.getCount("province", province);
            dataInfos.add(new DataInfo(province, (long) count));
        }
        return AjaxResult.success(dataInfos);
    }


    @GetMapping("/product/category/rank")
    public AjaxResult getCategoryRank() {
        List<DataInfo> dataInfos = new ArrayList<>();
        Map<String, Long> map = productService.selectProductCategoryRank();
        for (Map.Entry<String, Long> entry : map.entrySet()) {
            dataInfos.add(new DataInfo(entry.getKey(), entry.getValue()));
        }
        //再加几个模拟数据
        dataInfos.add(new DataInfo("生活用品", 100L));
        dataInfos.add(new DataInfo("虚拟商品", 150L));

        //按照value排序降序
//        dataInfos.sort(Comparator.comparing(DataInfo::getValue));

//        dataInfos.sort((o1, o2) -> o2.getValue().compareTo(o1.getValue()));

        return AjaxResult.success(dataInfos);
    }

    @GetMapping("/user/sex")
    public AjaxResult getSex() {
        List<DataInfo> dataInfos = new ArrayList<>();
        dataInfos.add(new DataInfo("男", (long) dataService.getCount("sex", "M")));
        dataInfos.add(new DataInfo("女", (long) dataService.getCount("sex", "F")));
        return AjaxResult.success(dataInfos);
    }

    @GetMapping("/user/age/{dt}")
    public AjaxResult getAge(@PathVariable String dt) {
        List<String> tags = Arrays.asList("50后", "60后", "70后", "80后", "90后", "00后", "10后");
        List<DataInfo> dataInfos = new ArrayList<>();
        for (String tag : tags) {
            long count = dataService.getTagCount(tag, dt);
            dataInfos.add(new DataInfo(tag, count));
        }
        return AjaxResult.success(dataInfos);
    }

    @GetMapping("/product/count")
    public AjaxResult getProductCount() {
        Map<String, Long> categoryCounts = productCategoryService.getCategoryCountsMap();

        List<DataInfo> dataInfos = new ArrayList<>();
        for (Map.Entry<String, Long> entry : categoryCounts.entrySet()) {
            dataInfos.add(new DataInfo(entry.getKey(), entry.getValue()));
        }
        //再加几个模拟数据
        dataInfos.add(new DataInfo("生活用品", 10L));
        dataInfos.add(new DataInfo("虚拟商品", 15L));

        return AjaxResult.success(dataInfos);
    }


    @GetMapping("/product/sale")
    public AjaxResult getProductSale() {
        List<DataInfo> dataInfos = new ArrayList<>();
        Map<String, Long> map = productService.selectProductCategoryRank();
        for (Map.Entry<String, Long> entry : map.entrySet()) {
            dataInfos.add(new DataInfo(entry.getKey(),entry.getValue()));
        }
        return AjaxResult.success(dataInfos);
    }


}
